package top.IT00544.novel.service;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import top.IT00544.novel.dao.NovelDao;
import top.IT00544.novel.entity.JsonMsg;
import top.IT00544.novel.entity.Novel;

import java.io.File;
import java.util.ArrayList;

/**
 * 小说服务层
 */
@Service
public class NovelService {

//    private static final Log logger = LogFactory.getLog(NovelService.class);
    NovelDao novelDao = new NovelDao();

    static{
        NovelService novelService = new NovelService();
        novelService.createTableNovel();
    }
    /**
     * 查询小说某章节内容
     * @param nov url_db_name,id
     * @return
     */
    public JsonMsg getChapterContent(Novel nov){
        JsonMsg jm = new JsonMsg();
        if(StringUtils.isEmpty(nov.getUrl_db_name())){
            jm.setStatus(JsonMsg.STATUS_ERR);
            jm.setMessage("url_db_name不能为空");
            return jm;
        }
//        if(nov.getId() == -1){
//            novel.setMessage("章节ID不能为空");
//            return novel;
//        }
        Novel novel = novelDao.getChapterContent(nov);
        jm.putData(novel);
        return jm;
    }
    /**
     * 查询小说所有章节
     * @param nov url_db_name
     * @return
     */
    public JsonMsg getCatalogs(Novel nov){
        JsonMsg jm = new JsonMsg();
        if(StringUtils.isEmpty(nov.getUrl_db_name())){
            jm.setStatus(JsonMsg.STATUS_ERR);
            jm.setMessage("url_db_name不能为空");
            return jm;
        }
        ArrayList<Novel> catalogs = novelDao.getCatalogs(nov);
        jm.putData(catalogs);
        return jm;
    }
    /**
     * 创建小说表
     */
    public JsonMsg createTableNovel(){
        JsonMsg jm = new JsonMsg();
        novelDao.createTableNovel();
        return jm;
    }

    /**
     * 查询全部小说信息
     * @return
     */
    public JsonMsg novelInfo(){
        JsonMsg jm = new JsonMsg();
        ArrayList<Novel> novels = novelDao.novelInfo();
        jm.putData(novels);
        return jm;
    }
    /**
     * 新增小说
     * <br>1.保存小说信息
     * <br>2.导入小说到数据库
     * @param novel url_local
     */
    public JsonMsg novelInsert(Novel novel){
        JsonMsg jm = new JsonMsg();
        if(StringUtils.isEmpty(novel.getUrl_local())){
            jm.setStatus(JsonMsg.STATUS_ERR);
            jm.setMessage("小说地址url_local不能为空");
            return jm;
        }
        File f = new File(novel.getUrl_local());
        if(!f.exists()){
            jm.setStatus(JsonMsg.STATUS_ERR);
            jm.setMessage("对应路径的小说不存在");
            return jm;
        }
        novel.setName(f.getName());
        novel.setUrl_db_name(novel.getName()+".db");
        if(f.length() < 211){
            jm.setStatus(JsonMsg.STATUS_ERR);
            jm.setMessage("小说大小(单位:byte)不能小于211");
            return jm;
        }
        novel.setSize((int)f.length());
        novel.setId(System.currentTimeMillis());
        //判断该小说文件是否可以被解析
        jm = totalChapterNum(novel, true);
        if(JsonMsg.STATUS_ERR.equals(jm.getStatus())){
            return jm;
        }
        //开始新增
        novelDao.novelInsert(novel);
        return jm;
    }

    /**
     * 读取文本文件目录
     * @param novel url_local
     * @param readSome true 读取前128KB内容的目录，false 读取全部目录
     * @return
     */
    public JsonMsg totalChapterNum(Novel novel,boolean readSome){
        JsonMsg jm = new JsonMsg();
        if(StringUtils.isEmpty(novel.getUrl_local())){
            jm.setStatus(JsonMsg.STATUS_ERR);
            jm.setMessage("小说地址url_local不能为空");
            return jm;
        }
        jm = novelDao.totalChapterNum(novel,readSome);
        int chapterNum = (int) jm.getData().get("chapterNum");
        if(chapterNum == 0){
            jm.setStatus(JsonMsg.STATUS_ERR);
            jm.setMessage("文件编码方式或章节命名方式不支持");
            return jm;
        }
        return jm;
    }
    /**
     * 删除小说
     * @param novel id小说ID
     */
    public JsonMsg novelDelete(Novel novel){
        JsonMsg jm = new JsonMsg();
        novelDao.novelDelete(novel);
        return jm;
    }
}
